package com.example.demo.controller;

import com.example.demo.common.JwtUtils;
import com.example.demo.common.ResultModel;
import com.example.demo.common.ResultModelTool;
import com.example.demo.entity.Notice;
import com.example.demo.entity.User;
import com.example.demo.service.NoticeService;

import com.example.demo.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Positive;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/notices")
@Transactional
public class NoticeController {
    @Resource
    NoticeService noticeService;

    @Resource
    private UserService userService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private JwtUtils jwtUtils;


    /**
     * 通知新增
     * @param notice
     * @return
     */
    @PostMapping("")
    @ResponseBody

    public ResultModel insert(@RequestBody Notice notice){

        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1=userService.getUser(userId);
        if(user1==null){
            return new ResultModel(200,"权限不足或者token过期");
        }
        ResultModel resultmodel=new ResultModel();
        resultmodel.setCode(200);
        noticeService.insertnotice(notice);
        resultmodel.setData("添加成功");
        return ResultModelTool.handleResultModel(resultmodel);
    }

    /**
     * 通知列表
     * @return
     */
    @GetMapping("")
    @ResponseBody
    public ResultModel selectAll(){
        List<Notice> list=noticeService.selectAll();
        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        resultModel.setData(list);
        return ResultModelTool.handleResultModel(resultModel);
    }

    /**
     * 单个通知查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ResponseBody
    public ResultModel selectid(@PathVariable(value = "id") @Positive(message = "{id}") int id){



        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        resultModel.setData(noticeService.selectid(id));
        return ResultModelTool.handleResultModel(resultModel);
    }

    /**
     * 通知修改
     * @param notice
     * @return
     */
    @PutMapping ("")
    @ResponseBody
    public ResultModel updateid( @RequestBody Notice notice){

        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1=userService.getUser(userId);
        if(user1==null){
            return new ResultModel(200,"权限不足或者token过期");
        }
        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        noticeService.updateid(notice);
        resultModel.setData("修改成功");
        return ResultModelTool.handleResultModel(resultModel);
    }

    /**
     * 通知删除
     * @param id
     * @return删除时为2 没删除时为1
     */

    @DeleteMapping("/{id}")
    @ResponseBody
    public ResultModel deleteid(@PathVariable(value = "id") @Positive(message = "{id}") int id){

        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1=userService.getUser(userId);
        if(user1==null){
            return new ResultModel(200,"token过期或者权限不足");
        }
        noticeService.deleteid(id);
        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        noticeService.deleteid(id);
        resultModel.setData("删除成功");
        return ResultModelTool.handleResultModel(resultModel);

    }

    /**
     * 将Excel文件中的数据批量导入到数据库中
     * @param myfile
     * @return
     * @throws Exception
     */

    @RequestMapping(value="/importExcel",method= RequestMethod.POST)
    @ResponseBody
    public ResultModel importExcel(@RequestParam("myfile") MultipartFile myfile) throws Exception {
        noticeService.importExcel(myfile);
        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        resultModel.setData("导入成功");
        return ResultModelTool.handleResultModel(resultModel);

    }

    /**
     * poi将数据库中的数据批量导出到Excel
     * @param response
     * @throws Exception
     */
    @RequestMapping(value="/exportExcel",method=RequestMethod.GET)
    @ResponseBody
    public void exportExcel(HttpServletResponse response) throws Exception {
        noticeService.exportExcel(response);
    }

    /**
     * 批量删除
     * @param arr
     * @return
     */

//
//    @DeleteMapping("deleteid")
//    @ResponseBody
//    public String deleteid(@RequestBody int arr[]) {
//        for (int a : arr) {
//            noticeService.deleteid(a);
//            //return "删除成功";
//        } //[5,6]
//        return "删除失败";
//    }

}
